/*
 * Copyright 2010-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.lysa.ooo.config;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.h2.jdbcx.JdbcConnectionPool;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Profile;
import org.springframework.context.config.AdviceMode;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;


/**
 * Greenhouse RDBMS access configuration.
 * A RDBMS provides the system of record for transactional data in the Greenhouse application.
 * We use {@link JdbcTemplate} to access that data.
 * We use compile-time-woven AspectJ-advice around {@link Transactional} methods to apply transaction management.
 * In "embedded mode", we use an embedded database to ease setup of a developer testing environment.
 * In "standard mode", we connect to a file-based H2 database via a connection pool.
 * @author Keith Donald
 */
//@Configuration
//@ImportResource("classpath:jpa-repository.xml")
public class DataConfig {

	/**
	 * Development Data configuration.
	 * @author Keith Donald
	 */
	//@Configuration
	//@Profile("development")
	static class Development {

		@Inject
		private Environment environment;
		
		//@Bean
		public DataSource dataSource() {
			ProxoolDataSource dataSource = new ProxoolDataSource();
			dataSource.setAlias(environment.getProperty("proxool.alias"));
			dataSource.setDriver(environment.getProperty("proxool.driver"));
			dataSource.setDriverUrl(environment.getProperty("proxool.url"));
			dataSource.setUser(environment.getProperty("proxool.username"));
			dataSource.setPassword(environment.getProperty("proxool.password"));
			dataSource.setDelegateProperties("characterEncoding="+environment.getProperty("proxool.characterEncoding"));
			dataSource.setMaximumConnectionCount(environment.getProperty("proxool.maximumConnectionCount", Integer.class));
			dataSource.setMinimumConnectionCount(environment.getProperty("proxool.minimumConnectionCount", Integer.class));
			dataSource.setSimultaneousBuildThrottle(environment.getProperty("proxool.simultaneousBuildThrottle", Integer.class));
			dataSource.setPrototypeCount(environment.getProperty("proxool.prototypeCount", Integer.class));
			return dataSource;
		}

	}
	
	/**
	 * Standard Data configuration.
	 * @author Keith Donald
	 */
	//@Configuration
	//@Profile("standard")
	static class Standard {

		@Inject
		private Environment environment;
		
	}
}
